package com.csu.jpetstore_cms.controller;

import com.csu.jpetstore_cms.common.CommonResponse;
import com.csu.jpetstore_cms.entity.Merchant;
import com.csu.jpetstore_cms.service.MerchantService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Valid
public class MerchantController {
    @Resource
    private MerchantService merchantService;


    /*
       用户注册
     */
    @PostMapping("/merchants")
    public CommonResponse<Merchant> register(@Validated @RequestBody Merchant merchant) {
        merchant.setIsAdmin(false);
        merchant.setStatus("Active");
        merchant.setCreatedAt(new Date());
        return merchantService.addNewMerchant(merchant);
    }
    /*
       用户登录
     */
//    @PostMapping("/tokens")
//    public CommonResponse<Merchant> login(@RequestParam("username") @NotBlank(message = "用户名不能为空") String account,
//                                          @RequestParam("password") @NotBlank(message = "密码不能为空") String password) {
//        //todo:需要使用会话跟踪技术
//
//        return merchantService.login(account, password);
//    }
    @PostMapping("/tokens")
    public CommonResponse<Merchant> login(@RequestBody Merchant merchant) {
        //todo:需要使用会话跟踪技术

        return merchantService.login(merchant.getUsername(), merchant.getPassword());
    }
    /*
        获取所有用户信息
     */
    @GetMapping("/merchants")
    public CommonResponse<List<Merchant>> getAllMerchant() {
        return merchantService.getAllMerchants();
    }
}
